/**
 * 
 */
package com.xbang.attendance.web.action.system.product;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.xbang.attendance.entity.product.Product;
import com.xbang.attendance.entity.product.ProductCategory;
import com.xbang.attendance.service.ProductService;
import com.xbang.attendance.utils.FieldUtil;
import com.demo2do.lighturl.annotation.Action;
import com.xbang.core.struts.support.BaseActionSupport;

import enumeration.ProductStatus;

/**
 * @author dev.zhang
 * 
 */
public class ProductDetailMananger extends BaseActionSupport {

	private static final long serialVersionUID = -1532252860400494843L;

	@Autowired
	private ProductService productService;

	private Integer id;

	private Product product;
	private Map<String, String > fieldsMap;
	private List<ProductCategory> categorys = new ArrayList<ProductCategory>();
	private String [] fields; 

	/**
	 * 增加产品明细
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action("/system/product/add")
	public String add() throws Exception {

		categorys = productService.getObjects(ProductCategory.class);

		return "detail";
	}

	@Action("/system/product/toexport")
	public String toExport(){
		//setFieldsMap(FieldUtil.productFields);
		return "field";
	}
	
	@Action("/system/product/export")
	public String export() throws Exception {
		String webPath =ServletActionContext.getRequest().getRealPath("//download//product//detail");
		HttpServletRequest request = ServletActionContext.getRequest();
		String urlPath = request.getRequestURL().toString();
		String contextPath = request.getContextPath();
		String contextURL = urlPath.split(contextPath)[0] + contextPath;
		String exportFile = contextURL + "/download/product/detail/" + productService.genericExcelFile(webPath, fields);
		return "r:" + exportFile;
		//return "index";
	}

	
	
	@Action("/system/product/${id}/edit")
	public String edit() throws Exception {
		if (id != null) {
			categorys = productService.getObjects(ProductCategory.class);
			product = (Product) productService.getObject(Product.class, id);
		}
		return "detail";
	}

	@Action("/product/detail/${id}/view")
	public String view() throws Exception {
		if (id != null) {
			product = (Product) productService.getObject(Product.class, id);
		}
		return "detail";
	}

	@Action("/system/product/save")
	public String save() throws Exception {

		if (null != product) {
			if (product.getId() == null) {
				product.setProductStatus(ProductStatus.input);
				productService.save(product);
			} else {
				productService.update(product);
			}
			
		}

		return "r:/system/product/"+currPageId+"/index/";
	}

	/**
	 * @return the id
	 */
	public Integer getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(Integer id) {
		this.id = id;
	}

	/**
	 * @return the product
	 */
	public Product getProduct() {
		return product;
	}

	/**
	 * @param product
	 *            the product to set
	 */
	public void setProduct(Product product) {
		this.product = product;
	}

	/**
	 * @return the categorys
	 */
	public List<ProductCategory> getCategorys() {
		return categorys;
	}

	/**
	 * @param categorys
	 *            the categorys to set
	 */
	public void setCategorys(List<ProductCategory> categorys) {
		this.categorys = categorys;
	}

	/**
	 * @param productService
	 *            the productService to set
	 */
	public void setProductService(ProductService productService) {
		this.productService = productService;
	}

	public Map<String, String> getFieldsMap() {
		return fieldsMap;
	}

	public void setFieldsMap(Map<String, String> fieldsMap) {
		this.fieldsMap = fieldsMap;
	}

	public String[] getFields() {
		return fields;
	}

	public void setFields(String[] fields) {
		this.fields = fields;
	}
	
}
